package com.gqj.meter.controller;

import com.alibaba.fastjson.JSON;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.StorageUtil;
import com.gqj.common.utils.poi.ExcelUtil;
import com.gqj.meter.domain.MdStorageAreaInfo;
import com.gqj.meter.domain.vo.MdStorageAreaInfoVo;
import com.gqj.meter.service.IMdStorageAreaInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 存放区信息Controller
 *
 * @author lxq
 * @date lxq-01-09
 */
@Api(value = "/meter/mdStorageAreaInfo", description = "存放区信息")
@RestController
@RequestMapping("/meter/mdStorageAreaInfo")
public class MdStorageAreaInfoController extends BaseController {
    @Autowired
    private IMdStorageAreaInfoService mdStorageAreaInfoService;
    @Value("${downloadPath}")
    private String downloadPath;

    /**
     * 查询存放区信息列表
     */
    @ApiOperation("查询存放区信息列表")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdStorageAreaInfo mdStorageAreaInfo) {
        startPage();
        List<MdStorageAreaInfo> list = mdStorageAreaInfoService.selectMdStorageAreaInfoList(mdStorageAreaInfo);
        return getDataTable(list);
    }

    /**
     * 导出存放区信息列表
     */
    @ApiOperation("导出存放区信息列表")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:export')")
    @Log(title = "存放区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdStorageAreaInfo mdStorageAreaInfo) {
        List<MdStorageAreaInfo> list = mdStorageAreaInfoService.selectMdStorageAreaInfoList(mdStorageAreaInfo);
        ExcelUtil<MdStorageAreaInfo> util = new ExcelUtil<MdStorageAreaInfo>(MdStorageAreaInfo.class);
        util.exportExcel(response, list, "存放区信息数据");
    }

    /**
     * 获取存放区信息详细信息
     */
    @ApiOperation("获取存放区信息详细信息")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:query')")
    @GetMapping(value = "/{storeAreaId}")
    public AjaxResult getInfo(@PathVariable("storeAreaId") Long storeAreaId) {
        return success(mdStorageAreaInfoService.selectMdStorageAreaInfoByStoreAreaId(storeAreaId));
    }

    /**
     * 新增存放区信息
     */
    @ApiOperation("新增存放区信息")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:add')")
    @Log(title = "存放区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdStorageAreaInfo mdStorageAreaInfo) {
        if (null == mdStorageAreaInfo.getStoreAreaId() || 0 == mdStorageAreaInfo.getStoreAreaId()) {
            return AjaxResult.error("新增失败，存放区ID为空！");
        }
        MdStorageAreaInfo tempEntity = mdStorageAreaInfoService.selectMdStorageAreaInfoByStoreAreaId(mdStorageAreaInfo.getStoreAreaId());
        if (null != tempEntity) {
            return AjaxResult.error("新增'" + mdStorageAreaInfo.getStoreAreaName() + "'失败，存放区ID已存在！");
        }
        if (UserConstants.NOT_UNIQUE.equals(mdStorageAreaInfoService.checkStoreAreaNoUnique(mdStorageAreaInfo))) {
            return AjaxResult.error("新增'" + mdStorageAreaInfo.getStoreAreaName() + "'失败，存放区编号已存在！");
        }

        return toAjax(mdStorageAreaInfoService.insertMdStorageAreaInfo(mdStorageAreaInfo));
    }

    /**
     * 修改存放区信息
     */
    @ApiOperation("修改存放区信息")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:edit')")
    @Log(title = "存放区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdStorageAreaInfo mdStorageAreaInfo) {
        if (null == mdStorageAreaInfo.getStoreAreaId() || 0 == mdStorageAreaInfo.getStoreAreaId()) {
            return AjaxResult.error("修改失败，存放区ID为空！");
        }
        if (UserConstants.NOT_UNIQUE.equals(mdStorageAreaInfoService.checkStoreAreaNoUnique(mdStorageAreaInfo))) {
            return AjaxResult.error("修改'" + mdStorageAreaInfo.getStoreAreaName() + "'失败，存放区编号已存在！");
        }
        return toAjax(mdStorageAreaInfoService.updateMdStorageAreaInfo(mdStorageAreaInfo));
    }

    /**
     * 删除存放区信息
     */
    @ApiOperation("删除存放区信息")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:remove')")
    @Log(title = "存放区信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeAreaIds}")
    public AjaxResult remove(@PathVariable Long[] storeAreaIds) {
        return toAjax(mdStorageAreaInfoService.deleteMdStorageAreaInfoByStoreAreaIds(storeAreaIds));
    }

    /**
     * 查询存放区下拉框
     */
    @ApiOperation("查询存放区下拉框")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:mdStorageAreaInfoOptions')")
    @GetMapping("/mdStorageAreaInfoOptions")
    public AjaxResult mdStorageAreaInfoOptions(MdStorageAreaInfo mdStorageAreaInfo) {
        List<Map<String, Object>> list = mdStorageAreaInfoService.mdStorageAreaInfoOptions(mdStorageAreaInfo);
        return AjaxResult.success(list);
    }

    /**
     * 下载存放区json文件
     */
    @ApiOperation("下载存放区json文件")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:downloadJsonMdStorageAreaInfo')")
    @GetMapping("/downloadJsonMdStorageAreaInfo")
    public AjaxResult downloadJsonMdStorageAreaInfo(MdStorageAreaInfo mdStorageAreaInfo) {
        List<MdStorageAreaInfo> sai = mdStorageAreaInfoService.selectMdStorageAreaInfoList(mdStorageAreaInfo);
        List<MdStorageAreaInfoVo> vos = StorageUtil.copyList(sai, MdStorageAreaInfoVo::new);

        String orderNo = StorageUtil.getOrderNoTo12();
        // 生成文件名
        String fileName = downloadPath;
        String jsonStringAc = JSON.toJSONString(vos);
        StorageUtil.createJsonFile(jsonStringAc, fileName, "存放区数据" + orderNo);
        return AjaxResult.success(fileName + "存放区数据" + orderNo);
    }

    @ApiOperation("下载存放区模板")
    @PostMapping("/importTemplateMdStorageAreaInfo")
    @PreAuthorize("@ss.hasPermi('storage:mdStorageAreaInfo:importTemplateMdStorageAreaInfo')")
    public void importTemplateMdStorageAreaInfo(HttpServletResponse response) {
        ExcelUtil<MdStorageAreaInfo> util = new ExcelUtil<MdStorageAreaInfo>(MdStorageAreaInfo.class);
        util.importTemplateExcel(response, "存放区数据");
    }

    /**
     * 导入存放区execl文件
     */
    @ApiOperation("导入存放区execl文件")
    @Log(title = "导入存放区xecl文件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('storage:mdWhAreaInfo:importDataMdStorageAreaInfo')")
    @PostMapping("/importDataMdStorageAreaInfo")
    public AjaxResult importDataMdStorageAreaInfo(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MdStorageAreaInfo> util = new ExcelUtil<MdStorageAreaInfo>(MdStorageAreaInfo.class);
        List<MdStorageAreaInfo> mdStorageAreaInfoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = mdStorageAreaInfoService.importMdStorageAreaInfo(mdStorageAreaInfoList, updateSupport, operName);
        return success(message);
    }
}
